package gameSite;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import gameSite.RemoteConnection;

/**
 * Servlet implementation class DisplayInventory
 */
@WebServlet("/DisplayInventory")
public class DisplayInventory extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Connection conn;
	Statement stmt = null;
	ResultSet rs = null;  
	ArrayList<Product> displayinventory; 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DisplayInventory() {
        super();
        // TODO Auto-generated constructor stub
    }
//    private void getResults(String keyword){
//    	Statement stmt = null;
//    	ResultSet rs = null;
//    	try{
//    	stmt = RemoteConnection.getConnection().createStatement();
//	      String sql = " select Products.productID, gameName, console, typeOfGame,price from Products join Inventory on Inventory.productID = Products.productID; "
//	      		System.out.println(sql);
//    	}
    	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String result = getInventoryList();
		PrintWriter out = response.getWriter();
		out.print(result);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}


/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */


protected String getInventoryList(){
	try{
		//STEP 4: Execute a query
		String sql, result="";
		int count = 0;
		displayinventory = new ArrayList<Product>();
		Product inventory;
	      System.out.println("Creating statement...");
	      stmt = RemoteConnection.getConnection().createStatement();
	      sql = "select Products.productID as ID, gameName, console, typeOfGame,price,numberOfProducts from Products join Inventory on Inventory.productID = Products.productID";
	      rs = stmt.executeQuery(sql);
	      while (rs.next()){
	    	  inventory = new Product();
	    	  inventory.setProductID(Integer.parseInt(rs.getString("ID")));
	    	  inventory.setGameName(rs.getString("gameName"));
	    	  inventory.setConsole(rs.getString("console"));
	    	  inventory.setTypeOFGame(rs.getString("typeofGame"));
	    	  inventory.setPrice(Double.parseDouble(rs.getString("price")));
	    	  inventory.setNumberOfProducts(Integer.parseInt(rs.getString("numberOfProducts")));
	    	  displayinventory.add(inventory);
	      }
	    	  rs.close();
		      stmt.close();
		      RemoteConnection.closeConnection();
		      for(Product c : displayinventory){   	  	      
			      result += "<tr class=\"data\">" +
							"<td class=\"data\" width=\"30\">"+ (++count)+ "</td>" +
							"<td class=\"data\"><a href=\"EditItem?type=2&id="+c.getProductID()+"\">"
									+ c.getProductID()+"</a></td>" +
							"<td class=\"data\"><label >"+c.getGameName()+"</label></td>" +
							"<td class=\"data\"><label >"+c.getConsole()+"</label></td>" +
							"<td class=\"data\"><label >"+c.getTypeOfGame()+"</label></td>" +
							"<td class=\"data\"><label>"+c.getPrice()+"</label></td>" +
							"<td class=\"data\"><label>"+c.getNumberOfProducts()+"</label></td>" +
							"<td class=\"data\"><a href=\"EditItem?type=2&id="+c.getProductID()+"\">Edit</a></td>"+
							"</tr>";
		      }
		      System.out.println(result);
		      return result;
	}
	catch(SQLException se){
	      //Handle errors for JDBC
	      se.printStackTrace();
	   }
	return null;
	
}

}
